﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbData;

namespace CECB
{
    /// <summary>
    /// Interaction logic for SimpleMatrixCalculation.xaml
    /// </summary>
    public partial class SimpleMatrixCalculation : Window
    {
        cecbData.DSSEntities datacontext;
        public SimpleMatrixCalculation()
        {
            InitializeComponent();
            datacontext = new DSSEntities();
            ProjectAutocompleteTextBox.ItemsSource = datacontext.Projects;
            clear();
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ProjectAutocompleteTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }



        private void CalculateButton_MouseEnter(object sender, MouseEventArgs e)
        {
        }

        private void ProjectAutocompleteTextBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            string projectReferance = null;
            clear();
            Projects proj = (Projects)ProjectAutocompleteTextBox.SelectedItem;
            try
            {
                projectReferance = proj.proj_reference;
            }
            catch (Exception)
            {

                return;
            }


            cecbClasses.Data_Adapters.MatrixSimple.SimpleMatrixCalculation SM = new cecbClasses.Data_Adapters.MatrixSimple.SimpleMatrixCalculation();

            string isCorrect_CheckCorrectivity = SM.CheckCorrectivity(projectReferance).ToString();

            string isCorrect1 = SM.getPhysicalEnvironmentalAffection(projectReferance).ToString();  // variable to check the correctivity of getPhysicalEnvironmentalAffection method
            string isCorrect2 = SM.getEchologicallyAffectin(projectReferance).ToString();
            string isCorrect3 = SM.getSociologicallyAffection(projectReferance).ToString();

            if (isCorrect_CheckCorrectivity == "-1")
            {
                MessageBox.Show("No values has been asign to the selected project", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);

            }
            else if (isCorrect1 == "-1")
            {
                MessageBox.Show("Database has no values on physical impacts", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (isCorrect2 == "-1")
            {
                MessageBox.Show("Database has no values on Echological impacts", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (isCorrect3 == "-1")
            {
                MessageBox.Show("Database has no values on sociological impacts", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {

                textBox_total.Text = SM.CheckCorrectivity(projectReferance).ToString();
                textBox_PhysicalImpact.Text = SM.getPhysicalEnvironmentalAffection(projectReferance).ToString();
                textBox_EchologicalImpact.Text = SM.getEchologicallyAffectin(projectReferance).ToString();
                textBox_SocialImpact.Text = SM.getSociologicallyAffection(projectReferance).ToString();
                label_sociologicalImpactPresentage.Content = SM.getSociologicallyAffectionPresentage(projectReferance).ToString();

                label_EchologicalImpactPresentage.Content = SM.getEchologicallyAffectinPresentage(projectReferance).ToString();
                label_physicalImpactPresentage.Content = SM.getPhysicalEnvironmentalAffectionPresentage(projectReferance).ToString();
                label_presentageMark1.Content = "%";
                label_presentageMark2.Content = "%";
                label_presentageMark3.Content = "%";

            }



        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void ReportButton_Click(object sender, RoutedEventArgs e)
        {


            try
            {
                Projects prj = (Projects)ProjectAutocompleteTextBox.SelectedItem;
                if (prj.proj_name.ToString().Length > 0)
                {

                    new MetrixResultsCharts(prj.proj_reference.ToString()).ShowDialog();
                }



            }
            catch (Exception ex)
            {
                if (ProjectAutocompleteTextBox.SelectedItem == null)
                {

                    MessageBox.Show("select a project first", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    MessageBox.Show("this function is currently unavailavle due to no values assign to this project", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

        }


        public void clear()
        {

            textBox_total.Text = "";
            textBox_PhysicalImpact.Text = "";
            textBox_EchologicalImpact.Text = "";
            textBox_SocialImpact.Text = "";
            label_sociologicalImpactPresentage.Content = "";

            label_EchologicalImpactPresentage.Content = "";
            label_physicalImpactPresentage.Content = "";
            label_presentageMark1.Content = "";
            label_presentageMark2.Content = "";
            label_presentageMark3.Content = "";

        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            new Matrix.SimpleMatrixFinalResult_Window().Show();
        }


    }
}
